package cn.hhy.org.controller;

import cn.hhy.org.domain.Department;
import cn.hhy.org.service.IDepartmentService;
import cn.hhy.org.query.DepartmentQuery;
import cn.hhy.utils.AjaxResult;
import cn.hhy.utils.QueryResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Department department) {
        if(department.getParent_id()==null||department.getParent_id()==0){
            department.setParent_id(0L) ;
            try {
                if (department.getId() != null) {
                    departmentService.update(department);
                } else {
                    departmentService.save(department);
                }

                return AjaxResult.me();
            } catch (Exception e) {
                e.printStackTrace();
                return AjaxResult.me().setSuccess(false).setMessage("保存失败！" + e.getMessage());
            }

        }else {
            try {
                if (department.getId() != null) {
                    departmentService.update(department);
                } else {
                    departmentService.save(department);
                }

                return AjaxResult.me();
            } catch (Exception e) {
                e.printStackTrace();
                return AjaxResult.me().setSuccess(false).setMessage("保存失败！" + e.getMessage());
            }
        }


    }

    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {

        try {
            departmentService.delete(id);


            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败！" + e.getMessage());
        }

    }
    @GetMapping("/{id}")
    public  Department loadById(@PathVariable("id") Long id){
        return departmentService.loadById(id);

    }
    @GetMapping("/employee")
    public  List<Department> loadById(){
        return departmentService.loadAll2();

    }
    @PostMapping
    public QueryResult<Department> loadAll(@RequestBody DepartmentQuery departmentQuery){
        System.out.println(departmentQuery);
        return departmentService.loadAll(departmentQuery);



    }
    @PatchMapping
    public AjaxResult PatchDelete(@RequestBody Long[] ids) {

        try {
            departmentService.PatchDelete(ids);

            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败！" + e.getMessage());
        }

    }

    @GetMapping
    public  List<Department> treeData(){
        List<Department> departments = departmentService.treeData();
        return departments;

    }




}
